/// 热门项目类型枚举
enum HotItemType {
  /// 图片类型
  image,

  /// 视频类型
  video,
}

/// 热门项目模型
class HotItem {
  /// 项目ID
  final String id;

  /// 发布用户
  final String publisherId;

  /// 项目类型
  final HotItemType type;

  /// 标题
  final String title;

  /// 详情描述
  final String description;

  /// 评论数
  final int commentCount;

  /// 点赞数
  final int likeCount;

  /// 资源地址
  final String resourceUrl;

  /// 缩略图地址（视频类型时使用）
  final String? thumbnailUrl;

  /// 是否推荐
  final bool isRecommended;

  const HotItem({
    required this.id,
    required this.publisherId,
    required this.type,
    required this.title,
    required this.description,
    required this.commentCount,
    required this.likeCount,
    required this.resourceUrl,
    this.thumbnailUrl,
    required this.isRecommended,
  });

  /// 从JSON创建HotItem实例
  factory HotItem.fromJson(Map<String, dynamic> json) {
    return HotItem(
      id: json['id'] as String,
      publisherId: json['publisherId'] as String,
      type: HotItemType.values.firstWhere(
        (e) => e.name == json['type'],
        orElse: () => HotItemType.image,
      ),
      title: json['title'] as String,
      description: json['description'] as String,
      commentCount: json['commentCount'] as int? ?? 0,
      likeCount: json['likeCount'] as int? ?? 0,
      resourceUrl: json['resourceUrl'] as String,
      thumbnailUrl: json['thumbnailUrl'] as String?,
      isRecommended: json['isRecommended'] as bool? ?? false,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'publisherId': publisherId,
      'type': type.name,
      'title': title,
      'description': description,
      'commentCount': commentCount,
      'likeCount': likeCount,
      'resourceUrl': resourceUrl,
      'thumbnailUrl': thumbnailUrl,
      'isRecommended': isRecommended,
    };
  }

  /// 复制并修改部分属性
  HotItem copyWith({
    String? id,
    String? publisherId,
    HotItemType? type,
    String? title,
    String? description,
    int? commentCount,
    int? likeCount,
    String? resourceUrl,
    String? thumbnailUrl,
    bool? isRecommended,
  }) {
    return HotItem(
      id: id ?? this.id,
      publisherId: publisherId ?? this.publisherId,
      type: type ?? this.type,
      title: title ?? this.title,
      description: description ?? this.description,
      commentCount: commentCount ?? this.commentCount,
      likeCount: likeCount ?? this.likeCount,
      resourceUrl: resourceUrl ?? this.resourceUrl,
      thumbnailUrl: thumbnailUrl ?? this.thumbnailUrl,
      isRecommended: isRecommended ?? this.isRecommended,
    );
  }

  bool get isVideo => type == HotItemType.video;

  bool get isImage => type == HotItemType.image;

  String get displayImageUrl {
    if (isVideo && thumbnailUrl != null) {
      return thumbnailUrl!;
    }
    return resourceUrl;
  }

  String get formattedLikeCount {
    if (likeCount >= 10000) {
      return '${(likeCount / 10000).toStringAsFixed(1)}w';
    } else if (likeCount >= 1000) {
      return '${(likeCount / 1000).toStringAsFixed(1)}k';
    }
    return likeCount.toString();
  }

  String get formattedCommentCount {
    if (commentCount >= 10000) {
      return '${(commentCount / 10000).toStringAsFixed(1)}w';
    } else if (commentCount >= 1000) {
      return '${(commentCount / 1000).toStringAsFixed(1)}k';
    }
    return commentCount.toString();
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is HotItem && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'HotItem(id: $id, publisherId: $publisherId, type: $type, title: $title, '
        'commentCount: $commentCount, likeCount: $likeCount, isRecommended: $isRecommended)';
  }
}
